import { ref } from 'vue'
import { defineStore } from 'pinia'
import { getLoginUserUsingPost } from '@/api/userController.ts'

export const useUserLoginStore = defineStore('loginUser', () => {
  const loginUser = ref<API.UserVO>({
    userName: '未登录',
  })

  function setLoginUser(newLoginUser: { userName: string; id: number | undefined }) {
    loginUser.value = newLoginUser
  }

  /**
   * 远程获取用户信息
   */
  async function fetchLoginUser() {
    const res = await getLoginUserUsingPost();
    if (res.data.code === 0 && res?.data?.data) {
      loginUser.value = res.data.data;
    }
  }

  return { loginUser, setLoginUser, fetchLoginUser }
})
